package com.ithinks.flow.common.utils.system;

import java.io.Serializable;

/**
 * @Description
 * @ClassName RuntimeInfoUtils
 * @Author MR.LI
 * @date 2020.11.19 19:20
 */
public class RuntimeInfoUtils implements Serializable {

    //初始化当前运行环境
    private final Runtime currentRuntime = Runtime.getRuntime();

    //获取当前运行环境
    public final Runtime getRuntime() {
        return currentRuntime;
    }

    //获取最大内存
    public final long getMaxMemory() {return currentRuntime.maxMemory()/1024/1024;}

    //获取空闲内存
    public final long getFreeMemory() {return currentRuntime.freeMemory()/1024/1024;}

    //获取总内存
    public final long getTotalMemory() {return currentRuntime.totalMemory()/1024/1024;}

    //获取总内存
    public final int getAvailableProcessors() {return currentRuntime.availableProcessors();}

    //获取可用内存
    public final long getUsableMemory() {
        return (currentRuntime.maxMemory() - currentRuntime.totalMemory() + currentRuntime.freeMemory())/1024/1024;
    }

    @Override
    public String toString() {
        return "RuntimeInfoUtils{\n" +
                "最大内存 ：" + getMaxMemory() +"M;\n"+
                "空闲内存 ：" + getFreeMemory() +"M;\n"+
                "进程内存 ：" + getTotalMemory() +"M;\n"+
                "可用内存 ：" + getUsableMemory() +"M;\n"+
                '}';
    }

    public static void main(String[] args) {
        System.out.println(new RuntimeInfoUtils().toString());
    }
}
